package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;


    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;


    @RequestMapping("")
    public String index(Model model){

        List<Region> regions = regionService.queryHotRegions();
        model.addAttribute("hotRegions",regions);

        return "destination/index";
    }

    @RequestMapping("getHotDestByRegionId")
    public Object getHotDestByRegionId(Long regionId,Model model){
        List<Destination> list = destinationService.selectHotDestByRegionId(regionId);

        //左边
        List<Destination> left = list.subList(0, list.size() / 2);

        //右边
        List<Destination> right = list.subList(list.size() / 2,list.size() );

        model.addAttribute("leftDests",left);
        model.addAttribute("rightDests",right);
        model.addAttribute("regionId",regionId);

        return "destination/hotdestTpl";
    }


    //guide
    @RequestMapping("guide")
    public Object guide(Long id,Model model){

        //吐司操作
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        List<StrategyCatalog> catalogs = strategyCatalogService.queryByDestId(id);
        model.addAttribute("catalogs",catalogs);

        //查询前三篇攻略

        List<StrategyDetail> strategyDetails = strategyDetailService.selectStrategyDetailByDeptId(id);
        model.addAttribute("strategyDetails",strategyDetails);


        return "destination/guide";
    }

    //surveyPage 查看概况 分类明细
    @RequestMapping("surveyPage")
    public String surveyPage(@ModelAttribute("qo")StrategyCatalogQuery qo,Model model){

        List<Destination> toasts = destinationService.getToasts(qo.getDestId());

        Destination dest = toasts.remove(toasts.size() - 1);

        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        return "destination/survey";
    }


    //survey
    @RequestMapping("survey")
    public String survey(@ModelAttribute("qo")StrategyCatalogQuery qo,Model model){

        List<StrategyCatalog> catalogs = strategyCatalogService.queryByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);

        StrategyCatalog catalog =null;

        for (StrategyCatalog c : catalogs) {
            if (c.getId()==qo.getCatalogId()){
                catalog = c;
                model.addAttribute("catalog",catalog);
            }
        }

        if (catalog!=null){

            List<StrategyDetail> details = catalog.getDetails();

            if (details!=null && details.size()>0 ){
                StrategyDetail detail = details.get(0);

                detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));

                model.addAttribute("detail",details.get(0));

            }
        }
        return "destination/surveyTpl";
    }


    //多种筛选下的文章列表
    @RequestMapping("travels")
    public Object travels(Model model, @ModelAttribute("qo")TravelQuery qo){
        model.addAttribute("pageInfo", travelService.query(qo));
        return "destination/travelTpl";
    }






}
